#!/bin/bash

# 定义变量
REDIS_CONF="/etc/redis/redis.conf"
FDFS_STORAGE_CONF="/etc/fdfs/storage.conf"
FDFS_TRACKER_CONF="/etc/fdfs/tracker.conf"
NGINX_SBIN="/usr/local/nginx/sbin/nginx"

cur_dir=$(realpath "$(dirname "$0")")
cd "${cur_dir}"

# 微服务目录
BASE_PATH="${cur_dir}"

# 定义函数
run_service() {
    cd $BASE_PATH/$1
    go run . &
    if [ $? -ne 0 ]; then
        echo "Failed to start $1"
        exit 1
    fi
}

# 启动redis
# redis-cli -h 127.0.0.1 -p 6379 --raw
sudo redis-server $REDIS_CONF || exit 1

# 启动mysql
# mysql -uroot -p123456
# source init.sql
# source home.sql;

# 启动etcd
etcd &

# 启动fastdfs存储服务器 
sudo /usr/bin/fdfs_storaged $FDFS_STORAGE_CONF restart || exit 1

# 启动fastdfs监视服务器 
sudo /usr/bin/fdfs_trackerd $FDFS_TRACKER_CONF restart || exit 1

# 启动nginx
sudo $NGINX_SBIN || exit 1

# 启动微服务
run_service "service/user"
run_service "service/getCaptcha"
run_service "service/house"
run_service "service/order"
run_service "web"
